$(function () {
    //跳转指定跳转回调
    document.getElementById("redirect-input").addEventListener("redirect-event",function (evt) {
        var detail = evt.detail;
        var parentId = detail.parentId;
        var parentEm = $($('#top-menu-list-box>li[data-id="'+parentId+'"]')[0]);
        parentEm.attr('data-redirect-one',detail.oneId);
        parentEm.data('redirect-one',detail.oneId);
        parentEm.attr('data-redirect-two',detail.twoId);
        parentEm.data('redirect-two',detail.twoId);
        parentEm.attr('data-redirect-uri',detail.uri);
        parentEm.data('redirect-uri',detail.uri);
        parentEm.click();
    });
    var appointJumpData = null;
    //监听点击面包屑
    document.getElementById("jump-box-input").addEventListener("jump-event",function (evt) {
        var detail = evt.detail;
        var id = detail.id;
        var type = detail.type;
        if(type==1){
            $('#top-menu-list-box>li[data-id="'+id+'"]').click();
        }else if(type==2){
            $($('#side-menu .user-function-li[data-id="'+id+'"]>a').eq(0)).click();
        }
    });

    init();

    /**
     * 初始化
     */
    function init() {
        $.ajax({
            url:baseloaction +'/admin/manage/findAllSysUserFunction',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    initMenu(result.entity);
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,"系统错误，初始化系统菜单失败");
            }
        });
    }

    /**
     * 初始化菜单
     * @param allFunctionList 所有的权限菜单列表
     */
    function initMenu(allFunctionList){
        //顶部权限菜单内容
        var context = '';
        //把ID当KEY，对象本身当Value的权限Map对象
        var allFunctionMap = {};
        //初始化ID
        var _parentFunObj = null;

        //获取顶部的HTML内容
        $.each(allFunctionList,function (index,parentFunObj) {
            allFunctionMap[parentFunObj.id] = parentFunObj;
            var className = '',_disabled='',title='';
            if(parentFunObj.hasFunction==false){//如没有权限的添加disabled class
                _disabled = 'disabled';
                title = '没有操作权限';
            }else{
                if(_parentFunObj ==null){//获取首次初始化的顶部权限对象
                    className = 'active';
                    _parentFunObj = parentFunObj;
                }
            }
            context +='<li class="'+className+' '+_disabled+'" data-name="'+parentFunObj.functionName+'" data-id="'+parentFunObj.id+'" data-index="'+index+'" data-redirect-one="0" data-redirect-two="0" data-redirect-uri="">';
            context +='<a title="'+title+'" href="javascript:void(0);">'+parentFunObj.functionName+'</a>';
            context +='</li>';
        });
        //设置顶部权限HTML内容
        $("#top-menu-list-box").html(context);
        appointJumpData =null;// {'oneId':307,'twoId':291,'uri':'/admin/exam/record/findTaskExamRecordList'};
        //第一次初始化左则菜单
        initLeftMenu(_parentFunObj);

        //初始化顶部权限菜单点击事件
        $("#top-menu-list-box>li").click(function () {
            var _this = $(this);
            if(_this.hasClass('disabled')){//如果没有权限禁止点击
                var name = _this.data('name');
                icModale(1,'没有【'+name+'】操作权限，如果需要更多操作权限请联系管理员');
                return;
            }
            //切换样式
            _this.addClass('active').siblings().removeClass('active');
            //得到ID
            var initParentId = _this.data('id');
            //得到顶部的权限对象
            _parentFunObj = allFunctionMap[initParentId];

            //获取最有指定跳转标识ID
            var oneId = _this.data('redirect-one');
            if(oneId==0){//没有指定跳转
                //初始化左则菜单
                initLeftMenu(_parentFunObj);
            }else{//有指定跳转
                var twoId = _this.data('redirect-two');
                var uri = _this.data('redirect-uri');
                //指定跳转对象数据
                appointJumpData = {};
                appointJumpData['oneId'] = oneId;
                appointJumpData['twoId'] = twoId;
                appointJumpData['uri'] = uri;
                //初始化左则菜单
                initLeftMenu(_parentFunObj);
                //指定跳转只使用一次，用过了就要清空属性值
                _this.data('redirect-one',0);
                _this.data('redirect-two',0);
                _this.data('redirect-uri','');
            }
        });
    }

    /**
     * 初始化左则菜单
     * @param parentFunObj 顶部权限对象
     */
    function initLeftMenu(parentFunObj){
        $("#side-menu .user-function-li").remove();
        if(parentFunObj==null || typeof  parentFunObj=='undefined'){
            return;
        }
        //左则菜单列表
        var leftList = parentFunObj.childSysFunctionList;
        if(leftList==null || leftList.length <= 0 || typeof  leftList=='undefined'){
            return;
        }
        //左则权限菜单HTML内容
        var leftMenuContext = '';
        //当前初始初始化选中的左则菜单对象
        var _leftFunObj = null;
        var leftFunMap = {};
        var selectStatus=false;
        var rightIndex = 0;
        var initRightIndex = 0;
        //左则一级菜单是否设置有点击时默认显示右菜单的下标状态对象
        var leftGetRightIndexStatus= {};
        //循环左则权限菜单列表
        $.each(leftList,function (index,leftFunObj) {
            leftFunMap[leftFunObj.id] = leftFunObj;
            var active = '';
            //左则子级菜单操作
            var _context ='<ul class="nav nav-second-level collapse in ad-nav-second-level clearfix">';
            //获取左则权限的子级权限列表
            var _childList = leftFunObj.childSysFunctionList;
            if(_childList !=null && _childList.length > 0){
                active ='';
                var addStatus = false;
                $.each(_childList,function (i,f) {
                    if(f.leftShow==1){//显示在左则下的子级权限
                        _disabled =''; title = '';
                        if(f.hasFunction==false){
                            _disabled = 'disabled';
                            title = '没有操作权限';
                        }
                        if(appointJumpData !=null && f.id == appointJumpData.twoId){
                            active = 'active';
                            initRightIndex = i;
                        }else{
                            if(f.hasFunction==true && selectStatus==false){
                                active = 'active';
                                selectStatus = true;
                                rightIndex = i;
                                initRightIndex = i;
                            }
                        }
                        _context +='<li class="left-child-box '+active+' '+_disabled+'" data-name="'+f.functionName+'" data-parent="'+f.parentId+'" data-id="'+f.id+'" data-index="'+i+'">';
                        _context +='<a title="'+title+'" href="javascript:;" class="J_menuItem">';
                        _context +=f.functionName;
                        _context +='</a>';
                        _context +='</li>';
                        addStatus = true;
                        active = '';
                    }else{
                        if(initRightIndex==0 && appointJumpData!=null && f.id == appointJumpData.twoId){
                            initRightIndex = i;
                        }else if(initRightIndex==0 && f.hasFunction==true && selectStatus==false){
                            active = 'active';
                            selectStatus = true;
                            rightIndex = i;
                            initRightIndex = i;
                        }
                    }
                    if(selectStatus==true){
                        //初始化未选中的左则菜单点击时默认显示的右则的菜单的下标
                        if(f.hasFunction==true && rightIndex <= i && leftGetRightIndexStatus[leftFunObj.id] !=true){
                            rightIndex = i;
                            leftGetRightIndexStatus[leftFunObj.id] = true;
                        }
                    }
                });
            }
            _context +='</ul>';

            if(appointJumpData!=null){//如果有指定跳转数据
                if(leftFunObj.id == appointJumpData.oneId){
                    _leftFunObj = leftFunObj;
                    active = 'active';
                }
            }else{//如果没有指定跳转数据，获取获取第一个有权限的左则父权限对象
                if(leftFunObj.hasFunction == true && _leftFunObj == null){
                    _leftFunObj = leftFunObj;
                    active = 'active';
                }
            }
            var _disabled = '',title = '';
            if(leftFunObj.hasFunction==false){
                _disabled = 'disabled';
                title ='没有操作权限';
            }
            leftMenuContext +='<li data-id="'+leftFunObj.id+'" data-name="'+leftFunObj.functionName+'" data-right-index="'+rightIndex+'" class="'+active+' user-function-li '+_disabled+'">';
            leftMenuContext +='<a title="'+title+'" href="javascript:void(0)" data-id="0">';
            var faClassName = 'fa-file';
            if(leftFunObj.iconClass !=null && $.trim(leftFunObj.iconClass) !=''){
                faClassName = leftFunObj.iconClass;
            }
            leftMenuContext +='<i class="fa '+faClassName+'"></i> ';
            leftMenuContext +='<span class="nav-label">'+leftFunObj.functionName+'</span>';
            leftMenuContext +='<span class="fa arrow"></span>';
            leftMenuContext +=' </a>';

            //如果有子级标签添加，则需要添加子级HTML内容
            if(addStatus){
                leftMenuContext += _context;
            }
            leftMenuContext +='</li>';
            rightIndex = 0;
        });
        //添加左则菜单权限HTML内容
        $("#side-menu").append(leftMenuContext);

        //原来左则被点击的父ID
        var rightNowParentId = 0;

        //左则一级菜单点击
        $("#side-menu .user-function-li>a").click(function () {
            var _this = $(this);
            if(_this.parent().hasClass('disabled')){
                var name = _this.parent().data('name');
                icModale(1,'没有【'+name+'】操作权限，如果需要更多操作权限请联系管理员');
                return;
            }
            //设置父级选中样式
            _this.parent().addClass('active').siblings().removeClass('active');
            //删除所有的子级的选中样式
            $(".left-child-box").removeClass('active');
            //初始化的右则菜单下标
            var _initRightIndex = _this.parent().data('right-index');
            //获取二级的IP
            rightNowParentId = _this.parent().data('id');
            //重新获取二级的权限对象
            _leftFunObj = leftFunMap[rightNowParentId];
            //初始化右则权限菜单
            intiRightMenu(_initRightIndex,_leftFunObj);
        });

        //左则二菜单点击
        $("#side-menu .left-child-box").click(function () {
            var _this = $(this);
            if(_this.hasClass('disabled')){
                var name = _this.data('name');
                icModale(1,'没有【'+name+'】操作权限，如果需要更多操作权限请联系管理员');
                return;
            }
            var parent = $(_this.parents('.user-function-li')[0]);
            parent.addClass('active').siblings().removeClass('active');;
            $(".left-child-box").removeClass('active');
            _this.addClass('active');
            var parentId = _this.data('parent');
            var index = _this.data('index');
         //   if(parentId == rightNowParentId){//如果还是原来的父级，则直接点击
         //       $("#J_menuTab_wrap>a").eq(index).click();
         //      $("#J_menuTab_wrap>a").removeClass('active').eq(index).addClass('active');
         //   }else{//如果不是原来的父级，则需要重新初始化右则导航内容
                _leftFunObj = leftFunMap[parentId];
                //初始化右则权限菜单
                intiRightMenu(index,_leftFunObj,null);
           // }
            rightNowParentId = parentId;
        });
        //初始化右则权限菜单
        intiRightMenu(initRightIndex,_leftFunObj);
    }

    /**
     * 初始化右则菜单
     * @param initRightIndex 初始化下标
     * @param leftFunObj 左则权限对象
     */
    function intiRightMenu(initRightIndex,leftFunObj){
        //清空原有HTML内容
        $("#J_menuTab_wrap").html("");
        $("#content-main").html("");
        var rightList = leftFunObj.childSysFunctionList;
        var _rightFunObj = null;
        if(rightList !=null && rightList.length > 0){
            //右则导航HTML内容
            var rightContext = '';
            $.each(rightList,function (index,rightFunObj) {
                var active = '';
                if(appointJumpData == null){
                    if(rightFunObj.hasFunction==true && index == initRightIndex){
                        _rightFunObj = rightFunObj;
                        active = 'active';
                    }
                }else{
                    if(rightFunObj.id == appointJumpData.twoId){
                        _rightFunObj = rightFunObj;
                        active = 'active';
                    }
                }
                var _disabled = '';
                if(rightFunObj.hasFunction==false){
                    _disabled = 'disabled';
                }
                rightContext +='<a href="javascript:;" class="'+active+' J_menuTab '+_disabled+'" data-fname="'+rightFunObj.functionName+'" data-fid="'+rightFunObj.id+'" data-id="'+(rightFunObj.functionUrl)+'" data-name="iframe'+index+'">'+rightFunObj.functionName+'</a>';
                if(active !=''){
                    $(".left-child-box").removeClass('active');
                    if($('.left-child-box[data-id="'+rightFunObj.id+'"]')){
                        $('.left-child-box[data-id="'+rightFunObj.id+'"]').addClass('active');
                    }
                }
            });
            $("#J_menuTab_wrap").html(rightContext);
        }
        initContentMain(initRightIndex,_rightFunObj);
    }

    /**
     * 初始化内容
     * @param index
     * @param rightFunObj 右则导航数据列表
     * @param parentEmData 根级元素跳转数据对象
     */
    function initContentMain(index,rightFunObj){
        var iframe = '<iframe class="J_iframe iframe0" name="iframe0" fId="0" width="100%" height="100%" src="" frameborder="no" border="0" data-id="" seamless></iframe>'
        iframe = $(iframe);
        var functionUrl = baseloaction + rightFunObj.functionUrl;
        if(appointJumpData !=null){
            functionUrl = appointJumpData.uri;
        }
        var iframeName = 'iframe'+index;
        iframe.attr({
            'src': functionUrl,
            'data-id': functionUrl,
            'name': iframeName,
            'fid': rightFunObj.id,
            'class': ''
        }).addClass('J_iframe '+iframeName);
        $('#content-main').empty().html(iframe);
        //清空定位数据
        appointJumpData = null;
    }
});